﻿using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections;
using System.Security;
using System.Security.Permissions;
using System.Reflection;
using System.Collections.Specialized;

namespace SharePoint.ImportExport.Core 
{
	/// <summary>
	/// The FieldType enumeration specifies a field type for a field.
	/// </summary>
	public enum FieldType
	{
		/// <summary>
		/// Contains attachments.
		/// </summary>
		Attachments,
		/// <summary>
		/// Contains Boolean values that are stored in the database as 1 or 0.
		/// </summary>
		Boolean,
		/// <summary>
		/// Contains calculated values.
		/// </summary>
		Calculated, 
		/// <summary>
		/// Specifies a predetermined set of values that can be used to enter data into the field.
		/// </summary>
		Choice,
		/// <summary>
		/// Specifies an abstract field type that depends on other fields for its content and definition.
		/// </summary>
		Computed,
		/// <summary>
		/// Contains an integer used for internal ID fields. 
		/// </summary>
		Counter,
		/// <summary>
		/// Specifies a link between projects in a Meetings Workspace site.
		/// </summary>
		CrossProjectLink,
		/// <summary>
		/// Contains currency values formatted based on a specific locale.
		/// </summary>
		Currency,
		/// <summary>
		/// Contains date and time values. 
		/// </summary>
		DateTime,
		/// <summary>
		/// Contains errors.  
		/// </summary>
		Error,
		/// <summary>
		/// Contains files.  
		/// </summary>
		File,
		/// <summary>
		/// Specifies a Choice field for a data sheet.  
		/// </summary>
		GridChoice, 
		/// <summary>
		/// Contains GUIDs.  
		/// </summary>
		Guid, 
		/// <summary>
		/// Contains positive or negative integer values.
		/// </summary>
		Integer,
		/// <summary>
		/// Non valid
		/// </summary>
		Invalid,
		/// <summary>
		/// Contains references to values in other lists. 
		/// </summary>
		Lookup, 
		/// <summary>
		/// Contains the maximum number of items.
		/// </summary>
		MaxItems,  
		/// <summary>
		/// Specifies Content Approval status.
		/// </summary>
		ModStat,  
		/// <summary>
		/// Contains multiple values per list item.
		/// </summary>
		MultiChoice,  
		/// <summary>
		/// Specifies a field that can contain multiple lines of text. 
		/// </summary>
		Note, 
		/// <summary>
		/// Contains floating point numbers. 
		/// </summary>
		Number, 
		/// <summary>
		/// Specifies a field used in calendars for recurring events and, like computed fields, an abstract field type that depends on other fields for its content and definition. 
		/// </summary>
		Recurrence,
		/// <summary>
		/// Contains a single line of text.
		/// </summary>
		Text,  
		/// <summary>
		/// Specifies a field that is used in the creation and display of threaded Web discussions. 
		/// </summary>
		Threading, 
		/// <summary>
		/// Contains hyperlinks. 
		/// </summary>
		URL,
		/// <summary>
		/// Specifies users of a SharePoint site. 
		/// </summary>
		User 

	}

  /// <summary>
	/// This is the main class for creating the list templates. 
	/// </summary>
	/// <remarks>There has to be exactly one instance of this class for each listtemplates.xml file
	/// <seealso cref="SharePoint.ImportExport.Core.DataContainer"/> and <seealso cref="SharePoint.ImportExport.Core.List"/>
	/// <example>
	/// <code>
	/// /* Create a new instance of the ListTemplates class*/
	/// SharePoint.ImportExport.Core.ListTemplatesList listtemplates= new ListTemplatesList();
	/// 
	/// /* Create a new instance of the ListTemplate class and add it to the ListTemplate collection */
	/// ListTemplate listTemplate= new ListTemplate();
	/// listtemplates.ListTemplates.Add(listTemplate);
	/// 
	/// listTemplate.NameAttribute="My Document Library";
	/// listTemplate.Name="My Document Library";
	/// listTemplate.Title="My Document Library";
	/// listTemplate.BaseType="DocumentLibrary";
	/// listTemplate.Description="My docs....";
	/// listTemplate.EnableAttachments=false;
	/// listTemplate.EnableVersioning=true;
	/// //Add a new custom field called MyField and add it fo the Fields collection
	/// 
	/// Field f=new Field();
	/// f.Name="MyField";
	/// f.Displayname="MyField";
	/// f.AddToDefaultView=true;
	/// f.Extendedtype=FieldType.Text;
	/// f.GroupBy=false;
	/// f.Required=false;
	/// f.Richtext=false;
	/// listTemplate.Fields.Add(f);
	/// 
	/// f=new Field();
	/// f.Name="MyUrlField";
	/// f.Displayname="MyUrlField";
	/// f.AddToDefaultView=true;
	/// f.Extendedtype=FieldType.URL;
	/// f.GroupBy=false;
	/// f.Required=false;
	/// f.Richtext=false;
	/// listTemplate.Fields.Add(f);
	/// 
	/// try
	///		{
	///			listtemplates.Serialize("listtemplates.xml");
	///		}
	///	catch(SPExportException ex)
	///		{
	///			Console.WriteLine("Exception occurred: "+ex.Message);
	///		}
	/// </code>
	/// </example>
	/// </remarks>
	
    [System.Xml.Serialization.XmlRootAttribute("listtemplates",Namespace="", IsNullable=false)]
    public class ListTemplatesList {
        
        private ListTemplatesCollection listTemplates;

			/// <summary>
			/// This is the list of list template nodes
			/// </summary>
			[System.Xml.Serialization.XmlElementAttribute("listtemplate", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			public ListTemplatesCollection ListTemplates
			{
				get
				{
					return listTemplates;
				}
			}
			

			private void ValidateField(Field field)
			{
				if(field.Displayname==null)
					throw new SPExportException("A field must have a displayname defined");
				if(field.Name==null)
					throw new SPExportException("A field must have a name defined");
				if(field.Extendedtype==FieldType.Invalid)
					throw new SPExportException("A field must have the extendedtype defined. For more info see the SPFieldType enumeration");
			}
			private void ValidateListTemplate(ListTemplate listTemplate)
			{
				if(listTemplate.Title==null)
					throw new SPExportException("A listtemplate node must have a title");
				if(listTemplate.Description==null)
					throw new SPExportException("A listtemplate node must have a description");
				if(listTemplate.Name==null)
					throw new SPExportException("A listtemplate node must have a name");
				if(listTemplate.BaseType==null)
					throw new SPExportException("A listtemplate node must have a basetype (See SharePoint SDK for info on SPListTemplate)");
				if(listTemplate.BaseType.IndexOf("Library")>0)
				{
					if(listTemplate.EnableAttachments)
						throw new SPExportException("The enableattachments option is only valid for lists");
				}
				else
				{
					if(listTemplate.EnableVersioning)
						throw new SPExportException("The enableversioning option is only valid for libraries");
				}
				if(listTemplate.Fields!=null)
				{
					foreach(Field field in listTemplate.Fields)
						try
						{
							ValidateField(field);
						}
						catch(SPExportException ex)
						{

							throw new SPExportException("Field validation in listtemplate "+listTemplate.Name + "failed. Error: "+ex.Message);
						}
				{
					}
				}
			}

			/// <summary>
			/// This is the serialization function for the listtemplates class
			/// </summary>
			/// <param name="fileName">this is the name of the XML file that will be generated</param>
			/// <returns></returns>
			/// <remarks>The method will throw as SPExportException if the data is invalid</remarks>
			public bool Serialize(string fileName)
			{
				string dirName=fileName;
				int pos=dirName.LastIndexOf("\\");
				if(pos>0)
					dirName=dirName.Substring(0,pos);
				if(pos!=-1) //Create the file in the working directory
				{
					if (!Directory.Exists(dirName))
						Directory.CreateDirectory(dirName);
				}

				if(!Validate())
				{
					throw new SPExportException(LastError);
				}

				bool returnValue=true;
				StreamWriter exportListFile = null;
				exportListFile = new StreamWriter(fileName,false);
				try
				{
					XmlSerializer mySerializer = new XmlSerializer(typeof(SharePoint.ImportExport.Core.ListTemplatesList));
					mySerializer.Serialize(exportListFile , this);
				}
				catch(IOException exSerialize)
				{
					Console.WriteLine("Exception error: "+exSerialize.Message);
					SPExportException ex= new SPExportException(exSerialize.Message);
					throw ex;
				}
				catch(Exception ex)
				{
					Console.WriteLine("Exception error: "+ex.Message);
					SPExportException ex2= new SPExportException(ex.Message);
					throw ex2;
				}
				finally
				{
					exportListFile .Flush();
					exportListFile .Close();
				}
				return returnValue;

			}
			/// <summary>
			/// This is the default constructor for the ListTemplates class
			/// </summary>
			/// <remarks>Instantiates the ListTemplate member</remarks>
			public ListTemplatesList()
			{
				listTemplates=new ListTemplatesCollection();
			}

			/// <summary>
			/// This is the validation method for the class
			/// </summary>
			public bool Validate()
			{
				bool returnValue=true;
				if(this.ListTemplates==null)
				{
					returnValue=false;
					lastError="The listtemplates.xml should contain at least one listtemplate node";
				}
				foreach(ListTemplate listTemplate in ListTemplates)
				{
					try
					{
						ValidateListTemplate(listTemplate);
					}
					catch(SPExportException ex)
					{
						returnValue=false;
						lastError=ex.Message;
					}
				}
				return returnValue;
			}

			private string lastError;

			/// <summary>
			/// This is the last error from the validation
			/// </summary>
			public string LastError
			{
				get
				{
					return lastError;
				}
			}
		}
	/// <summary>
	/// This is the class implementing the ListTemplates collection
	/// </summary>
	public class ListTemplatesCollection:ICollection
	{
		/// <summary>
		/// This is the default constructor for the ListTemplatesCollection class
		/// </summary>
		/// <remarks>Instantiates the listTemplates member</remarks>
		public ListTemplatesCollection()
		{
			listTemplates=new ArrayList();
		}
		private ArrayList listTemplates;
		#region ICollection Members

		/// <summary>
		/// Gets a value indicating whether access to the ArrayList is synchronized (thread-safe).
		/// </summary>
		/// <remarks>
		/// To guarantee the thread safety of the ArrayList, all operations must be done through the wrapper returned by the Synchronized method.
		/// Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
		/// </remarks>
		public bool IsSynchronized
		{
			get
			{
				
				return listTemplates.IsSynchronized;
			}
		}

		/// <summary>
		/// Returns a value indicating the number of objects in the collection. Read-only.
		/// </summary>
		public int Count
		{
			get
			{
				return listTemplates.Count;
			}
		}
		/// <summary>
		/// Copies the entire ListTemplate Array to a compatible one-dimensional Array, starting at the specified index of the target array.
		/// </summary>
		/// <param name="a">The one-dimensional Array that is the destination of the elements copied from ArrayList. The Array must have zero-based indexing. </param>
		/// <param name="index">The zero-based index in array at which copying begins</param>
		public void CopyTo(ListTemplate[] a, int index)
		{
			listTemplates.CopyTo(a, index);
		}

		/// <summary>
		/// Copies the entire ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.
		/// </summary>
		/// <param name="a">The one-dimensional Array that is the destination of the elements copied from ArrayList. The Array must have zero-based indexing. </param>
		/// <param name="index">The zero-based index in array at which copying begins</param>
		public void CopyTo(Array a, int index)
		{
			listTemplates.CopyTo(a, index);
		}

		/// <summary>
		/// Gets an object that can be used to synchronize access to the ArrayList.
		/// </summary>
		/// <remarks>
		/// To create a synchronized version of the ArrayList, use the Synchronized method. However, derived classes can provide their own synchronized version of the ArrayList using the SyncRoot property. The synchronizing code must perform operations on the SyncRoot of the ArrayList, not directly on the ArrayList. This ensures proper operation of collections that are derived from other objects. 
		/// Specifically, it maintains proper synchronization with other threads that might be simultaneously modifying the ArrayList object.
		/// Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or 
		/// catch the exceptions resulting from changes made by other threads.
		/// </remarks>
		public object SyncRoot
		{
			get
			{
				return listTemplates.SyncRoot;
			}
		}

		#endregion

		#region IEnumerable Members

		/// <summary>
		/// Returns an enumerator that can iterate through the ArrayList.
		/// </summary>
		/// <returns></returns>
		public IEnumerator GetEnumerator()
		{
			return listTemplates.GetEnumerator() ;
		}

		#endregion
		/// <summary>
		/// Gets the element at the specified index.
		/// </summary>
		public ListTemplate this[int index]
		{
			get{return (ListTemplate) listTemplates[index];}
		}
		/// <summary>
		/// Adds an object to the end of the ArrayList.
		/// </summary>
		/// <param name="newListTemplate">New ListTemplate to be added to the list</param>
		public void Add(ListTemplate  newListTemplate)
		{
			listTemplates.Add(newListTemplate);
		}

	}
    
    /// <summary>
    /// This is the class encapsulationg a list template
    /// </summary>
    /// <remarks><seealso cref="SharePoint.ImportExport.Core.ListTemplatesList"/></remarks>
    public class ListTemplate 
		{
        
        private string name;
			/// <summary>
			/// This is the name of the list template
			/// </summary>
			/// <remarks>This has to match the Name attribute in the Profile class<seealso cref="SharePoint.ImportExport.Core.Profile"/></remarks>
			[System.Xml.Serialization.XmlElementAttribute("name",Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			public string Name
			{
				get 
				{
					return name;
				}
				set
				{
					name=value;
				}
			}
        
        private string title;
        /// <summary>
        /// This is the title of the list template
        /// </summary>
			[System.Xml.Serialization.XmlElementAttribute("Title",Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			public string Title
			{
				get
				{
					return title;
				}
				set
				{
					title=value;
				}
			}
        
        private string baseType;
			/// <summary>
			/// This is the basetype of the list/library
			/// </summary>
			/// <remarks>
			/// Valid choices are
			/// <list type="bullet">
			/// <item>Announcements</item>
			/// <item>Contacts</item>
			/// <item>DiscussionBoard</item>
			/// <item>DocumentLibrary</item>
			/// <item>Events</item>
			/// <item>GenericList</item>
			/// <item>IssueTracking</item>
			/// <item>Links</item>
			/// <item>PictureLibrary</item>
			/// <item>Tasks</item>
			/// </list>
			/// </remarks>
			[System.Xml.Serialization.XmlElementAttribute("BaseType",Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			public string BaseType
			{
				get
				{
					return baseType;
				}
				set
				{
					baseType=value;
				}
			}
        
        private string description;
			/// <summary>
			/// This is the description of the list/library
			/// </summary>
			[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			public string Description
			{
				get
				{
					return description;
				}
				set
				{
					description=value;
				}
			}

        
        private bool enableAttachments;
			/// <summary>
			/// Boolean flag indicating whether or not the list supports attachments
			/// </summary>
			/// <remarks>Can only be true for lists</remarks>
			[System.Xml.Serialization.XmlElementAttribute("enableattachments",Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			public bool EnableAttachments
			{
				get
				{
					return enableAttachments;
				}
				set
				{
					enableAttachments=value;
				}
			}
			
        
        private bool enableVersioning;
			/// <summary>
			/// Boolean flag indicating whether or not the library supports versioning
			/// </summary>
			/// <remarks>Can only be true for libraries</remarks>
			/// 
			[System.Xml.Serialization.XmlElementAttribute("enableversioning",Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			public bool EnableVersioning
			{
				get
				{
					return enableVersioning;
				}
				set
				{
					enableVersioning=value;
				}
			}

        
        private FieldsCollection fields;
			/// <summary>
			/// This is a list of the metadata fields
			/// </summary>
			[System.Xml.Serialization.XmlArrayAttribute("fields",Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			[System.Xml.Serialization.XmlArrayItemAttribute("field",Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
			public FieldsCollection Fields
			{
				get
				{
					return fields;
				}
			}

        
        private ViewsCollection views;
			/// <summary>
			/// This is the list of the views 
			/// </summary>
			[System.Xml.Serialization.XmlArrayAttribute("views",Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			[System.Xml.Serialization.XmlArrayItemAttribute("view",Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
			public ViewsCollection Views
			{
				get
				{
					return views;
				}
			}

        
        private string nameAttribute;
			/// <summary>
			/// The name attribute of the list
			/// </summary>
			[System.Xml.Serialization.XmlAttributeAttribute("name")]
			public string NameAttribute
			{
				get
				{
					return nameAttribute;
				}
				set
				{
					nameAttribute=value;
				}
			}
			private string propertyBag;

			/// <summary>
			/// This will contain all the extra properties not included in the schema.xml
			/// </summary>
			[System.Xml.Serialization.XmlElementAttribute("propertyBag",Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			public string PropertyBag
			{
				get
				{
					return propertyBag;
				}
				set
				{
					propertyBag=value;
				}
			}
        
			/// <summary>
			/// This is the default constructor for the ListTemplate class
			/// </summary>
			/// <remarks>Instantiates the Fields and Views members</remarks>
			public ListTemplate()
			{
				views=new ViewsCollection();
				fields=new FieldsCollection();
			}
    }
  
  /// <summary>
  /// This is the class implementing the Fields collection
  /// </summary>
	public class FieldsCollection:ICollection
	{
		/// <summary>
		/// This is the default constructor for the FieldsCollection class
		/// </summary>
		/// <remarks>Instantiates the fields member</remarks>
		public FieldsCollection()
		{
			fields=new ArrayList();
		}
		private ArrayList fields;
		#region ICollection Members

		/// <summary>
		/// Gets a value indicating whether access to the ArrayList is synchronized (thread-safe).
		/// </summary>
		/// <remarks>
		/// To guarantee the thread safety of the ArrayList, all operations must be done through the wrapper returned by the Synchronized method.
		/// Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
		/// </remarks>
		public bool IsSynchronized
		{
			get
			{
				
				return fields.IsSynchronized;
			}
		}

		/// <summary>
		/// Returns a value indicating the number of objects in the collection. Read-only.
		/// </summary>
		public int Count
		{
			get
			{
				return fields.Count;
			}
		}
		/// <summary>
		/// Copies the entire Field Array to a compatible one-dimensional Array, starting at the specified index of the target array.
		/// </summary>
		/// <param name="a">The one-dimensional Array that is the destination of the elements copied from ArrayList. The Array must have zero-based indexing. </param>
		/// <param name="index">The zero-based index in array at which copying begins</param>
		public void CopyTo(Field[] a, int index)
		{
			fields.CopyTo(a, index);
		}


		/// <summary>
		/// Copies the entire ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.
		/// </summary>
		/// <param name="a">The one-dimensional Array that is the destination of the elements copied from ArrayList. The Array must have zero-based indexing. </param>
		/// <param name="index">The zero-based index in array at which copying begins</param>
		public void CopyTo(Array a, int index)
		{
			fields.CopyTo(a, index);
		}

		/// <summary>
		/// Gets an object that can be used to synchronize access to the ArrayList.
		/// </summary>
		/// <remarks>
		/// To create a synchronized version of the ArrayList, use the Synchronized method. However, derived classes can provide their own synchronized version of the ArrayList using the SyncRoot property. The synchronizing code must perform operations on the SyncRoot of the ArrayList, not directly on the ArrayList. This ensures proper operation of collections that are derived from other objects. 
		/// Specifically, it maintains proper synchronization with other threads that might be simultaneously modifying the ArrayList object.
		/// Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or 
		/// catch the exceptions resulting from changes made by other threads.
		/// </remarks>
		public object SyncRoot
		{
			get
			{
				return fields.SyncRoot;
			}
		}

		#endregion

		#region IEnumerable Members

		/// <summary>
		/// Returns an enumerator that can iterate through the ArrayList.
		/// </summary>
		/// <returns></returns>
		public IEnumerator GetEnumerator()
		{
			return fields.GetEnumerator() ;
		}

		#endregion
		/// <summary>
		/// Gets the element at the specified index.
		/// </summary>
		public Field this[int index]
		{
			get{return (Field) fields[index];}
		}
		/// <summary>
		/// Adds an object to the end of the ArrayList.
		/// </summary>
		/// <param name="newField">New Field to be added to the list</param>
		public void Add(Field  newField)
		{
			fields.Add(newField);
		}

	}
  
    /// <summary>
    /// This is the class encapsulating the field that will be added to the list/library
    /// </summary>
    public class Field 
		{
			/// <summary>
			/// Default constructor
			/// </summary>
			public Field()
			{
				propertyValues=new PropertyValuesCollection();
				extendedtype=FieldType.Invalid;
			}
			/// <summary>
			/// This is the name of the field
			/// </summary>
			private string name;

			/// <summary>
			/// This is the name of the field
			/// </summary>
			[System.Xml.Serialization.XmlElementAttribute("name", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			public string Name
			{
				get
				{
					return name;
				}
				set
				{
					//ToDo add validation
					name=value;
				}
			}

			/// <summary>
			/// This is the displayname of the field
			/// </summary>
			private string displayname;
			/// <summary>
			/// This is the displayname of the field
			/// </summary>
			[System.Xml.Serialization.XmlElementAttribute("displayname", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			public string Displayname
			{
				get
				{
					return displayname;
				}
				set
				{
					displayname=value;
				}
			}
			private FieldType extendedtype;
        
			/// <summary>
			/// This is the base type that the field is based on
			/// </summary>
			/// <remarks>
			/// Valid types
			/// <list type="bullet">
			/// <item>Boolean</item>
			/// <item>Calculated</item>
			/// <item>Choice</item>
			/// <item>Currency</item>
			/// <item>DateTime</item>
			/// <item>Guid</item>
			/// <item>Integer</item>
			/// <item>Lookup</item>
			/// <item>MultiChoice</item>
			/// <item>Note</item>
			/// <item>Number</item>
			/// <item>Recurrence</item>
			/// <item>Text</item>
			/// <item>Threading</item>
			/// <item>URL</item>
			/// <item>User</item>
			/// </list>
			/// </remarks>
			[System.Xml.Serialization.XmlElementAttribute("extended-type", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			public FieldType Extendedtype
			{
				get
				{
					return extendedtype;
				}
				set
				{
					extendedtype=value;
				}
			}

			private bool required;
			/// <summary>
			/// Boolean flag to indicate whether the field is mandatory or not
			/// </summary>
			[System.Xml.Serialization.XmlElementAttribute("required", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			public bool Required
			{
				get
				{
					return required;
				}
				set
				{
					required=value;
				}
			}

			/// <summary>
			/// Boolean flag to indicate whether the field is added to the default view
			/// </summary>
			private bool addToDefaultView;
			/// <summary>
			/// Boolean flag to indicate whether the field is added to the default view
			/// </summary>
			[System.Xml.Serialization.XmlElementAttribute("addtodefaultview", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			public bool AddToDefaultView
			{
				get
				{
					return addToDefaultView;
				}
				set
				{
					addToDefaultView=value;
				}
			}

			/// <summary>
			/// Boolean flag to indicate whether the field is of type RichText
			/// </summary>
			private bool richtext;
			/// <summary>
			/// Boolean flag to indicate whether the field is of type RichText
			/// </summary>
			[System.Xml.Serialization.XmlElementAttribute("richtext", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			public bool Richtext
			{
				get
				{
					return richtext;
				}
				set
				{
					richtext=value;
				}
			}
			/// <summary>
			/// Boolean flag to indicate whether the field is used as a group by criteria
			/// </summary>
			private bool groupBy;
			/// <summary>
			/// Boolean flag to indicate whether the field is used as a group by criteria
			/// </summary>
			[System.Xml.Serialization.XmlElementAttribute("groupby", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			public bool GroupBy
			{
				get
				{
					return groupBy;
				}
				set
				{
					groupBy=value;
				}
			
			}
			private PropertyValuesCollection propertyValues;
			/// <summary>
			/// This is the optional list of propertyvalues. Used when defining a choice type field
			/// </summary>
			[System.Xml.Serialization.XmlElementAttribute("propertyvalues", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			public PropertyValuesCollection PropertyValues
			{
				get 
				{
					return propertyValues;
				}
			}
			/// <summary>
			/// This is the displayname of the field
			/// </summary>
			private string xml;
			/// <summary>
			/// This is the displayname of the field
			/// </summary>
			[System.Xml.Serialization.XmlElementAttribute("schemaXML", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			public string Xml
			{
				get
				{
					return xml;
				}
				set
				{
					xml=value;
				}
			}

			private bool hidden=false;

			/// <summary>
			/// This controls the visibility of the field
			/// </summary>
			[System.Xml.Serialization.XmlElementAttribute("hidden",Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			public bool Hidden
			{
				get
				{
					return hidden;
				}
				set
				{
					hidden=value;
				}
			}

		}

	/// <summary>
	/// This is the class implementing the PropertyValues collection
	/// </summary>
	public class PropertyValuesCollection:ICollection
	{
		/// <summary>
		/// This is the default constructor for the PropertyValuesCollection class
		/// </summary>
		/// <remarks>Instantiates the PropertyValues member</remarks>
		public PropertyValuesCollection()
		{
			propertyValues=new ArrayList();
		}
		private ArrayList propertyValues;
		#region ICollection Members

		/// <summary>
		/// Gets a value indicating whether access to the ArrayList is synchronized (thread-safe).
		/// </summary>
		/// <remarks>
		/// To guarantee the thread safety of the ArrayList, all operations must be done through the wrapper returned by the Synchronized method.
		/// Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
		/// </remarks>
		public bool IsSynchronized
		{
			get
			{
				
				return propertyValues.IsSynchronized;
			}
		}
		/// <summary>
		/// Returns a value indicating the number of objects in the collection. Read-only.
		/// </summary>
		public int Count
		{
			get
			{
				return propertyValues.Count;
			}
		}
		/// <summary>
		/// Copies the entire String Array to a compatible one-dimensional Array, starting at the specified index of the target array.
		/// </summary>
		/// <param name="a">The one-dimensional Array that is the destination of the elements copied from ArrayList. The Array must have zero-based indexing. </param>
		/// <param name="index">The zero-based index in array at which copying begins</param>
		public void CopyTo(String[] a, int index)
		{
			propertyValues.CopyTo(a, index);
		}


		/// <summary>
		/// Copies the entire ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.
		/// </summary>
		/// <param name="a">The one-dimensional Array that is the destination of the elements copied from ArrayList. The Array must have zero-based indexing. </param>
		/// <param name="index">The zero-based index in array at which copying begins</param>
		public void CopyTo(Array a, int index)
		{
			propertyValues.CopyTo(a, index);
		}

		/// <summary>
		/// Gets an object that can be used to synchronize access to the ArrayList.
		/// </summary>
		/// <remarks>
		/// To create a synchronized version of the ArrayList, use the Synchronized method. However, derived classes can provide their own synchronized version of the ArrayList using the SyncRoot property. The synchronizing code must perform operations on the SyncRoot of the ArrayList, not directly on the ArrayList. This ensures proper operation of collections that are derived from other objects. 
		/// Specifically, it maintains proper synchronization with other threads that might be simultaneously modifying the ArrayList object.
		/// Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or 
		/// catch the exceptions resulting from changes made by other threads.
		/// </remarks>
		public object SyncRoot
		{
			get
			{
				return propertyValues.SyncRoot;
			}
		}

		#endregion

		#region IEnumerable Members

		/// <summary>
		/// Returns an enumerator that can iterate through the ArrayList.
		/// </summary>
		/// <returns></returns>
		public IEnumerator GetEnumerator()
		{
			return propertyValues.GetEnumerator() ;
		}

		#endregion
		/// <summary>
		/// Gets the element at the specified index.
		/// </summary>
		public String this[int index]
		{
			get{return (String) propertyValues[index];}
		}
		/// <summary>
		/// Adds an object to the end of the ArrayList.
		/// </summary>
		/// <param name="newPropertyValue">New PropertyValues to be added to the list</param>
		public void Add(String  newPropertyValue)
		{
			propertyValues.Add(newPropertyValue);
		}

	}

	/// <summary>
	/// This is the class implementing the Views collection
	/// </summary>
	public class ViewsCollection:ICollection
	{
		/// <summary>
		/// This is the default constructor for the ViewsCollection class
		/// </summary>
		/// <remarks>Instantiates the views member</remarks>

		public ViewsCollection()
		{
			views=new ArrayList();
		}
		private ArrayList views;
		#region ICollection Members

		/// <summary>
		/// Gets a value indicating whether access to the ArrayList is synchronized (thread-safe).
		/// </summary>
		/// <remarks>
		/// To guarantee the thread safety of the ArrayList, all operations must be done through the wrapper returned by the Synchronized method.
		/// Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
		/// </remarks>

		public bool IsSynchronized
		{
			get
			{
				
				return views.IsSynchronized;
			}
		}

		/// <summary>
		/// Returns a value indicating the number of objects in the collection. Read-only.
		/// </summary>
		public int Count
		{
			get
			{
				return views.Count;
			}
		}

		/// <summary>
		/// Copies the entire View array to a compatible one-dimensional Array, starting at the specified index of the target array.
		/// </summary>
		/// <param name="a">The one-dimensional Array that is the destination of the elements copied from ArrayList. The Array must have zero-based indexing. </param>
		/// <param name="index">The zero-based index in array at which copying begins</param>
		public void CopyTo(View[] a, int index)
		{
			views.CopyTo(a, index);
		}
		/// <summary>
		/// Copies the entire ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.
		/// </summary>
		/// <param name="a">The one-dimensional Array that is the destination of the elements copied from ArrayList. The Array must have zero-based indexing. </param>
		/// <param name="index">The zero-based index in array at which copying begins</param>
		public void CopyTo(Array a, int index)
		{
			views.CopyTo(a, index);
		}

		/// <summary>
		/// Gets an object that can be used to synchronize access to the ArrayList.
		/// </summary>
		/// <remarks>
		/// To create a synchronized version of the ArrayList, use the Synchronized method. However, derived classes can provide their own synchronized version of the ArrayList using the SyncRoot property. The synchronizing code must perform operations on the SyncRoot of the ArrayList, not directly on the ArrayList. This ensures proper operation of collections that are derived from other objects. 
		/// Specifically, it maintains proper synchronization with other threads that might be simultaneously modifying the ArrayList object.
		/// Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or 
		/// catch the exceptions resulting from changes made by other threads.
		/// </remarks>
		public object SyncRoot
		{
			get
			{
				return views.SyncRoot;
			}
		}

		#endregion

		#region IEnumerable Members

		/// <summary>
		/// Returns an enumerator that can iterate through the ArrayList.
		/// </summary>
		/// <returns></returns>
		public IEnumerator GetEnumerator()
		{
			return views.GetEnumerator() ;
		}

		#endregion
		/// <summary>
		/// Gets the element at the specified index.
		/// </summary>
		public View this[int index]
		{
			get{return (View) views[index];}
		}
		/// <summary>
		/// Adds an object to the end of the ArrayList.
		/// </summary>
		/// <param name="newView">New View to be added to the list</param>

		public void Add(View  newView)
		{
			views.Add(newView);
		}

	}
      
    /// <summary>
    /// This is the class encapsulating the view
    /// </summary>
    public class View 
		{
			/// <summary>
			/// Default constructor for the view
			/// </summary>
			public View()
			{
				viewFields=new StringCollection ();
			}
        
        /// <summary>
        /// This is the view name
        /// </summary>
        private string name;
			/// <summary>
			/// This is the view name
			/// </summary>
			[System.Xml.Serialization.XmlElementAttribute("name", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			public string Name
			{
				get
				{
					return name;
				}
				set
				{
					name=value;
				}
			}
        
		/// <summary>
		/// Boolean flag to indicate whether the view will become the default view
		/// </summary>
		private bool defaultView;
		/// <summary>
		/// Boolean flag to indicate whether the view will become the default view
		/// </summary>
		[System.Xml.Serialization.XmlElementAttribute("defaultview", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
		public bool DefaultView
		{
			get
			{
				return defaultView;
			}
			set
			{
				defaultView=value;
			}
		}
				
		/// <summary>
		/// Boolean value that specifies whether the view is hidden.
		/// </summary>
		private bool hidden;
		/// <summary>
		/// Boolean flag to indicate whether the view will become the default view
		/// </summary>
		[System.Xml.Serialization.XmlElementAttribute("hidden", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
		public bool Hidden
		{
			get
			{
				return hidden;
			}
			set
			{
				hidden=value;
			}
		}
				
        
        /// <summary>
        /// This is the query specified in CAML
        /// </summary>
        private string query;
			/// <summary>
			/// This is the query specified in CAML
			/// </summary>
			[System.Xml.Serialization.XmlElementAttribute("query", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			public string Query
			{
				get
				{
					return query;
				}
				set
				{
					query=value;
				}
			}
        
				private StringCollection viewFields;
			/// <summary>
			/// This is the collection of the field in the view
			/// </summary>
			[System.Xml.Serialization.XmlArrayAttribute("viewfields", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			[System.Xml.Serialization.XmlArrayItem("name", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
			
			public StringCollection ViewFields
			{
				get
				{
					return viewFields;
				}
			}



    }

//	/// <summary>
//	/// This is the class implementing the ViewFields collection
//	/// </summary>
//	public class ViewFieldsCollection:ICollection
//	{
//		/// <summary>
//		/// This is the default constructor for the ViewFieldsCollection class
//		/// </summary>
//		/// <remarks>Instantiates the viewFields member</remarks>
//
//		public ViewFieldsCollection()
//		{
//			viewFields=new ArrayList();
//		}
//		private ArrayList viewFields;
//		#region ICollection Members
//
//		/// <summary>
//		/// Gets a value indicating whether access to the ArrayList is synchronized (thread-safe).
//		/// </summary>
//		/// <remarks>
//		/// To guarantee the thread safety of the ArrayList, all operations must be done through the wrapper returned by the Synchronized method.
//		/// Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
//		/// </remarks>
//		public bool IsSynchronized
//		{
//			get
//			{
//				
//				return viewFields.IsSynchronized;
//			}
//		}
//
//		/// <summary>
//		/// Returns a value indicating the number of objects in the collection. Read-only.
//		/// </summary>
//		public int Count
//		{
//			get
//			{
//				return viewFields.Count;
//			}
//		}
//
//		/// <summary>
//		/// Copies the entire ViewField Array to a compatible one-dimensional Array, starting at the specified index of the target array.
//		/// </summary>
//		/// <param name="a">The one-dimensional Array that is the destination of the elements copied from ArrayList. The Array must have zero-based indexing. </param>
//		/// <param name="index">The zero-based index in array at which copying begins</param>
//		public void CopyTo(ViewField[] a, int index)
//		{
//			viewFields.CopyTo(a, index);
//		}
//		/// <summary>
//		/// Copies the entire ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.
//		/// </summary>
//		/// <param name="a">The one-dimensional Array that is the destination of the elements copied from ArrayList. The Array must have zero-based indexing. </param>
//		/// <param name="index">The zero-based index in array at which copying begins</param>
//		public void CopyTo(Array a, int index)
//		{
//			viewFields.CopyTo(a, index);
//		}
//
//		/// <summary>
//		/// Gets an object that can be used to synchronize access to the ArrayList.
//		/// </summary>
//		/// <remarks>
//		/// To create a synchronized version of the ArrayList, use the Synchronized method. However, derived classes can provide their own synchronized version of the ArrayList using the SyncRoot property. The synchronizing code must perform operations on the SyncRoot of the ArrayList, not directly on the ArrayList. This ensures proper operation of collections that are derived from other objects. 
//		/// Specifically, it maintains proper synchronization with other threads that might be simultaneously modifying the ArrayList object.
//		/// Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or 
//		/// catch the exceptions resulting from changes made by other threads.
//		/// </remarks>
//		public object SyncRoot
//		{
//			get
//			{
//				return viewFields.SyncRoot;
//			}
//		}
//
//		#endregion
//
//		#region IEnumerable Members
//
//		/// <summary>
//		/// Returns an enumerator that can iterate through the ArrayList.
//		/// </summary>
//		/// <returns></returns>
//		public IEnumerator GetEnumerator()
//		{
//			return viewFields.GetEnumerator() ;
//		}
//
//		#endregion
//		/// <summary>
//		/// Gets the element at the specified index.
//		/// </summary>
//		public ViewField this[int index]
//		{
//			get{return (ViewField) viewFields[index];}
//		}
//		/// <summary>
//		/// Adds an object to the end of the ArrayList.
//		/// </summary>
//		/// <param name="newViewField">New ViewField to be added to the list</param>
//
//		public void Add(ViewField  newViewField)
//		{
//			viewFields.Add(newViewField);
//		}
//
//	}

	/// <summary>
	/// This class implements the viewfields functionality
	/// </summary>
	public class ViewField 
	{
		/// <summary>
		/// 
		/// </summary>
		private string name;

		/// <summary>
		/// This is the view field name
		/// </summary>
		
		public string Name
		{
			get
			{
				return name;
			}
			set
			{
				name=value;
			}
		}
		/// <summary>
		/// Default constructor
		/// </summary>
		public ViewField()
		{
		}
		/// <summary>
		/// Constructor with initialization
		/// </summary>
		/// <param name="field">View field name</param>
		public ViewField(string field)
		{
			name=field;
		}

	}
	}
